package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.management.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
@SuppressWarnings("all")
public class DeptController {
    @Autowired
    private DeptService deptService;
    @PostMapping
    public Result addDept(@RequestBody Dept dept){
        log.info("新增部门，部门信息：{}",dept);
        deptService.addDept(dept);
        return Result.success();
    }

    @GetMapping
    public Result listDepts(
            @RequestParam(name = "name", required = false) String name,
            @RequestParam(name = "status", required = false)Integer status,
            @RequestParam(name = "page",defaultValue = "1")Integer page,
            @RequestParam(name = "pageSize", defaultValue = "10")Integer pageSize){
        log.info("查询部门列表，参数：name={},status={},page={},pageSize={}",name,status,page,pageSize);
        PageResult<Dept> pageResult = deptService.listDepts(name, status, page, pageSize);
        return Result.success(pageResult);
    }

    //根据Id查询部门
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据Id查询部门，部门Id：{}",id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }
    @GetMapping("/list")
    public Result getAllDepts(){
        log.info("查询所有部门");
        List<Dept> list = deptService.getAllDepts();
        return Result.success(list);
    }

    @PutMapping
    public Result updateDept(@RequestBody Dept dept){
        log.info("更新部门，部门信息：{}",dept);
        if(deptService.updateDept(dept)==1) return Result.success();
        else return Result.error("更新失败");
    }

    @DeleteMapping("/{id}")
    public Result deleteDept(@PathVariable Integer id){
        log.info("删除部门，部门Id：{}",id);
        if(deptService.deleteDept(id)==1) return Result.success();
        else return Result.error("删除失败");
    }


}